// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// A [author] rhyme_lph
// q [email]  rhymelph@gmail.com
// N [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/account_entity.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/utils/input_formatter_tools.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/form/single_file_form.dart';
import 'package:finance_app/src/widgets/input/input_name_field.dart';
import 'package:finance_app/src/widgets/list_item/value_border_Item.dart';
import 'package:finance_app/src/widgets/pop/normal_border_pop.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/all_reports/bank_reconciliation/views/reconcile_account_provider.dart';
import 'package:finance_app/src/provider/all_reports/bank_reconciliation/bank_reconciliation_provider.dart';

class ReconcileAccountView extends StatefulWidget {
  @override
  _ReconcileAccountViewState createState() => _ReconcileAccountViewState();
}

class _ReconcileAccountViewState extends State<ReconcileAccountView>
    with
        StatefulPresenter<ReconcileAccountView, BankReconciliationProvider,
            ReconcileAccountProvider>,
        KeepAliveMixin {
  @override
  Widget buildWidget(BuildContext context) => ColoredBox(
        color: Colors.white,
        child: ListView(
          children: [
            AppSpaces.h20,
            listBuilder<AccountEntity>(
              selector: (_, __) => __.currentAccount,
              builder:
                  (BuildContext context, AccountEntity value, Widget child) =>
                      NormalBorderPop<AccountEntity>(
                select: value,
                buildDisplayName: (e) =>
                    '${e.bankName} ${e.accountNumber ?? ''}',
                hint: S.current.choose_account,
                onSelect: provider.onSelectAccount,
                group: provider.data,
              ),
            ),
            listBuilder<bool>(
                selector: (_, __) => __.currentAccount != null,
                builder: (BuildContext context, bool value, Widget child) =>
                    Visibility(visible: value, child: child),
                child: ListView(
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  children: [
                    AppSpaces.h15,
                    listBuilder<String>(
                      selector: (_, __) =>
                          '${__.currentAccount?.currencyCode} ${__.currentAccount?.beginningBalance}',
                      builder:
                          (BuildContext context, String value, Widget child) =>
                              ValueBorderItem(
                        title: S.current.beginning_balance,
                        value: value,
                      ),
                    ),
                    AppSpaces.h15,
                    listBuilder<String>(
                      selector: (_, __) =>
                          DateTimeUtils.formatGlobalDateTime(__.openingDate),
                      builder:
                          (BuildContext context, String value, Widget child) =>
                              PressedOpacityButton(
                        onPressed: provider.onChangeClosingDate,
                        child: ValueBorderItem(
                          title: S.current.opening_date,
                          value: value,
                          valueColor: aceBlueColor,
                        ),
                      ),
                    ),
                    AppSpaces.h15,
                    listBuilder<String>(
                      selector: (_, __) =>
                          DateTimeUtils.formatGlobalDateTime(__.closingDate),
                      builder:
                          (BuildContext context, String value, Widget child) =>
                              PressedOpacityButton(
                        onPressed: provider.onChangeClosingDate,
                        child: ValueBorderItem(
                          title: S.current.closing_date,
                          value: value,
                          valueColor: aceBlueColor,
                        ),
                      ),
                    ),
                    AppSpaces.h15,
                    InputBorderField(
                      tip: S.current.statement_balance,
                      controller: provider.statementBalanceController,
                      focusNode: provider.statementBalanceFocusNode,
                      keyboardType:
                          TextInputType.numberWithOptions(decimal: true),
                      inputFormatters: [
                        doubleInputAllowNegativeFormatter,
                      ],
                    ),
                    AppSpaces.h15,
                    NormalFormWrapper(
                      leftPadding: getDp(50),
                      topPadding: 0.0,
                      bottomPadding: 0.0,
                      child: listBuilder<String>(
                        selector: (_, __) => __.file,
                        builder: (BuildContext context, String value,
                                Widget child) =>
                            SingleFileForm(
                          file: value,
                          onDelete: provider.onDeleteFile,
                          onAdd: provider.onAddFile,
                        ),
                      ),
                    ),
                    AppSpaces.h30,
                    NormalButton(
                      title: S.current.start,
                      onTap: provider.onStart,
                    ),
                    AppSpaces.h40,
                  ],
                )),
          ],
        ),
      );
}
